package models

import (
	"cdk.tf/go/stack/interfaces"
	// "your-project/interfaces"
)

// NetworkCreationRequest 网络创建请求
type NetworkCreationRequest struct {
	VpcName          string            `json:"vpc_name"`
	VpcCidr          string            `json:"vpc_cidr"`
	SubnetName       string            `json:"subnet_name"`
	SubnetCidr       string            `json:"subnet_cidr"`
	AvailabilityZone string            `json:"availability_zone"`
	Region           string            `json:"region"`
	Tags             map[string]string `json:"tags"`
}

// SecurityGroupCreationRequest 安全组创建请求
type SecurityGroupCreationRequest struct {
	Name         string                     `json:"name"`
	Description  string                     `json:"description"`
	VpcID        string                     `json:"vpc_id"`
	IngressRules []SecurityGroupRuleRequest `json:"ingress_rules"`
	EgressRules  []SecurityGroupRuleRequest `json:"egress_rules"`
	Tags         map[string]string          `json:"tags"`
}

type SecurityGroupRuleRequest struct {
	Description string   `json:"description"`
	FromPort    int      `json:"from_port"`
	ToPort      int      `json:"to_port"`
	Protocol    string   `json:"protocol"`
	CidrBlocks  []string `json:"cidr_blocks"`
}

// 转换方法
func (req *NetworkCreationRequest) ToVPCConfig() *interfaces.VPCConfig {
	return &interfaces.VPCConfig{
		Name:      req.VpcName,
		CidrBlock: req.VpcCidr,
		Region:    req.Region,
		Tags:      req.Tags,
	}
}

func (req *NetworkCreationRequest) ToSubnetConfig(vpcID string) *interfaces.SubnetConfig {
	return &interfaces.SubnetConfig{
		Name:                req.SubnetName,
		CidrBlock:           req.SubnetCidr,
		AvailabilityZone:    req.AvailabilityZone,
		VpcID:               vpcID,
		MapPublicIPOnLaunch: true,
		Tags:                req.Tags,
	}
}

func (req *SecurityGroupCreationRequest) ToSecurityGroupConfig() *interfaces.SecurityGroupConfig {
	ingressRules := make([]interfaces.SecurityGroupRule, len(req.IngressRules))
	for i, rule := range req.IngressRules {
		ingressRules[i] = interfaces.SecurityGroupRule{
			Description: rule.Description,
			FromPort:    rule.FromPort,
			ToPort:      rule.ToPort,
			Protocol:    rule.Protocol,
			CidrBlocks:  rule.CidrBlocks,
		}
	}

	egressRules := make([]interfaces.SecurityGroupRule, len(req.EgressRules))
	for i, rule := range req.EgressRules {
		egressRules[i] = interfaces.SecurityGroupRule{
			Description: rule.Description,
			FromPort:    rule.FromPort,
			ToPort:      rule.ToPort,
			Protocol:    rule.Protocol,
			CidrBlocks:  rule.CidrBlocks,
		}
	}

	return &interfaces.SecurityGroupConfig{
		Name:         req.Name,
		Description:  req.Description,
		VpcID:        req.VpcID,
		IngressRules: ingressRules,
		EgressRules:  egressRules,
		Tags:         req.Tags,
	}
}
